<?php
class ControllerProductReview extends Controller {
	private $error = array();
 
	public function index() {
		$this->load->language('product/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/review');
		$this->getList();
	}

	public function update() {
		$this->load->language('product/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/review');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_review->editReview($this->request->get['review_id'], $this->request->post['status']);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/review', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm();
	}

	private function getList() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_product_name'] = $this->language->get('column_product_name');
		$this->data['column_product_display_name'] = $this->language->get('column_product_display_name');
		$this->data['column_author'] = $this->language->get('column_author');
		$this->data['column_rating'] = $this->language->get('column_rating');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_action'] = $this->language->get('column_action');		
 
		$this->data['button_filter'] = $this->language->get('button_filter');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/review', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$url = '';
		if (isset($this->request->get['filter_product_display_name'])) {
			$filter_product_display_name = $this->request->get['filter_product_display_name'];
			$url .= '&filter_product_display_name=' . $this->request->get['filter_product_display_name'];
		} else {
			$filter_product_display_name = '';
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'r.date_added';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['reviews'] = array();
		$data = array(
			'filter_product_display_name'  => $filter_product_display_name,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$review_total = $this->model_product_review->getTotalReviews($data);
		$results = $this->model_product_review->getReviews($data);
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/review/update', 'token=' . $this->session->data['token'] . '&review_id=' . $result['review_id'], 'SSL')
			);
						
			$this->data['reviews'][] = array(
				'review_id'    => $result['review_id'],
				'product_name' => $result['product_name'],
				'product_display_name' => $result['product_display_name'],
				'author'       => $result['author'],
				'rating'       => $result['rating'],
				'status'       => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'date_added'   => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'action'       => $action
			);
		}	

		$url_sort = $url;
		if ($order == 'ASC') {
			$url_sort .= '&order=DESC';
		} else {
			$url_sort .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url_sort .= '&page=' . $this->request->get['page'];
		}
		$this->data['sort_product_display_name'] = $this->url->link('product/review', 'token=' . $this->session->data['token'] . '&sort=pd.display_name' . $url_sort, 'SSL');
		$this->data['sort_author'] = $this->url->link('product/review', 'token=' . $this->session->data['token'] . '&sort=r.author' . $url_sort, 'SSL');
		$this->data['sort_rating'] = $this->url->link('product/review', 'token=' . $this->session->data['token'] . '&sort=r.rating' . $url_sort, 'SSL');
		$this->data['sort_status'] = $this->url->link('product/review', 'token=' . $this->session->data['token'] . '&sort=r.status' . $url_sort, 'SSL');
		$this->data['sort_date_added'] = $this->url->link('product/review', 'token=' . $this->session->data['token'] . '&sort=r.date_added' . $url_sort, 'SSL');
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		$pagination = new Pagination();
		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/review', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['filter_product_display_name'] = $filter_product_display_name;
		$this->data['token'] = $this->session->data['token'];

		$this->template = 'product/review_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_select'] = $this->language->get('text_select');

		$this->data['entry_product_name'] = $this->language->get('entry_product_name');
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_content'] = $this->language->get('entry_content');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_video'] = $this->language->get('entry_video');
		$this->data['entry_rating'] = $this->language->get('entry_rating');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_customer_name'] = $this->language->get('entry_customer_name');
		$this->data['entry_customer_email'] = $this->language->get('entry_customer_email');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['token'] = $this->session->data['token'];
				
   		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/review', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['action'] = $this->url->link('product/review/update', 'token=' . $this->session->data['token'] . '&review_id=' . $this->request->get['review_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/review', 'token=' . $this->session->data['token'], 'SSL');

		$this->load->model('tool/image');
		$review_info = $this->model_product_review->getReview($this->request->get['review_id']);
		$this->data['product_name'] = $review_info['product_name'];
		$this->data['product_url'] = $this->url->linkProduct($review_info['product_id'], "review");
		$this->data['author'] = $review_info['author'];
		$this->data['content'] = $review_info['content'];
		if ($review_info['image'] && file_exists(DIR_IMAGE . $review_info['image'])) {
			$this->data['image'] = $this->model_tool_image->resize($review_info['image'], 100, 100);
		} else {
			$this->data['image'] = '';
		}
		if ($review_info['video_image'] && file_exists(DIR_IMAGE . $review_info['video_image'])) {
			$this->data['video_image'] = $this->model_tool_image->resize($review_info['video_image'], 100, 100);
		} else {
			$this->data['video_image'] = '';
		}
		$this->data['video_url'] = $review_info['video_url'];
		$this->data['rating'] = $review_info['rating'];
		$this->data['status'] = $review_info['status'];
		$this->data['firstname'] = $review_info['firstname'];
		$this->data['lastname'] = $review_info['lastname'];
		$this->data['email'] = $review_info['email'];
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		$this->template = 'product/review_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>